package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.service.StoreTongService;

public class StoreTongController extends BaseController{
	private StoreTongService storerTongService = Duang.duang(StoreTongService.class);
	/**
	 * 首页
	 */
	public void index(){
		int userid=getCurrentUser().getInt("id");
		if(storerTongService.findByUserId(userid)!=null){
				String sellerId=storerTongService.findByUserId(userid).get("sellerId");
	    		Record record=storerTongService.countStore(sellerId);
	    		setAttr("tolnumber", record.get("tolnumber"));
	    		setAttr("sjshnumber", record.get("sjshnumber"));
	    		setAttr("sjnumber", record.get("sjnumber"));
	    		setAttr("xjshnumber", record.get("xjshnumber"));
	    		setAttr("xjnumber", record.get("xjnumber"));
	    		List<Record> list=storerTongService.findAllStoreid(userid);
		    	setAttr("list", list);
	    		render("index.jsp");
	    	}
		else{
			
			render("index.jsp");
		}
	
	}
	/**
	 * 分页查询
	 */
	 public void page(){
		    int userid=getCurrentUser().getInt("id");
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			String storeIds = getPara("storeIds");
			String sStoreId = getPara("sStoreId");
			String sBeginDate = getPara("sBeginDate");
			String sEndDate = getPara("sEndDate");
			String sellerId=null;
			if(StrKit.notNull(storerTongService.findByUserId(userid))){
				 sellerId=storerTongService.findByUserId(userid).get("sellerId");
			}
			renderResult(0,storerTongService.findByPage(pageNum, pageSize, storeIds, sStoreId,sBeginDate,sEndDate,sellerId));
		}
	 
	 public void xspage(){
		    int userid=getCurrentUser().getInt("id");
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			String storeIds = getPara("storeIds");
			String sellerId=storerTongService.findByUserId(userid).get("sellerId");
			String sStoreId = getPara("sStoreId");
			String sBeginDate = getPara("sBeginDate");
			String sEndDate = getPara("sEndDate");
			int date =getParaToInt("date", 0);
			renderResult(0,storerTongService.findXSByPage(pageNum, pageSize, storeIds,sellerId, sStoreId,sBeginDate,sEndDate,date));
		}
	 
	    /**
	     * 查看分公司详情
	     */
	    public void detail(){
	    	String storeid=getPara("storeid");
			if(storeid!=null){
				setAttr("object", storerTongService.detail(storeid));
			}
			render("detail.jsp");
	    }
		/**
		 * 分公司图的添加图片的页面
		 */
		public void imageIndex(){
			String stroeimagenum =getPara(0);
			setAttr("stroeimagenum",stroeimagenum);
	        render("storeimageList.jsp");
	    }	
		/**
		 * 图片分页
		 */
	    public void imagePage(){
	    	String stroeimagenum =getPara(0);		
	    	renderSuccess(storerTongService.findAllImages(stroeimagenum));
	    }
		/**
		 * excel导出
		 */
		public void doExcel(){
		  String operationMsg = "excel导出";
		  logger.info(operationMsg);
			List<Pair> titles = new ArrayList<Pair>();
		    titles.add(new Pair("storeid", "分公司ID"));
		    titles.add(new Pair("title", "分公司名称"));
		    titles.add(new Pair("scope", "主营范围"));
		    titles.add(new Pair("createtime", "开店时间"));
		    titles.add(new Pair("aname", "分公司联系人"));
		    titles.add(new Pair("aphone", "联系电话"));
		    titles.add(new Pair("countGoods", "产品数量"));
		    titles.add(new Pair("countNum", "订单量"));
		    titles.add(new Pair("countMoney", "销售额"));
		    titles.add(new Pair("qstate", "分公司状态"));		    
		    int userid=getCurrentUser().getInt("id");
			int RoleId= storerTongService.findByRoleId(userid);
			String storeIds = getPara("storeIds");
			String sBeginDate = getPara("sBeginDate");
			String sEndDate = getPara("sEndDate");
			int date =getParaToInt("date", 0);
	 		String sStoreId = null;
	 		String sSellerId =null;
	 		if(RoleId==1||RoleId==2){
	 			 sSellerId =storerTongService.findByUserId(userid).get("sellerId");
	 			 sStoreId = getPara("sStoreId");
	 		}else{
	 			sStoreId =storerTongService.findStoreState(userid).get("storeid");
	 			}
			List<Record> list=storerTongService.findExcelByPage( storeIds,sSellerId, sStoreId,sBeginDate,sEndDate,date);
			List<Record> listz=new ArrayList<Record>();
			for (int i = 0; i < list.size(); i++) {
				Record record=new Record();
					record.set("storeid",list.get(i).get("storeid"));
					record.set("title", list.get(i).get("title"));
					if(list.get(i).get("scope").equals(1)){
						record.set("scope", "常规");
					}else if(list.get(i).get("scope").equals(2)){
						record.set("scope", "团购");
					}else{
						record.set("scope", "常规+团购");
					}
					record.set("createtime", list.get(i).get("createtime"));
					record.set("aname", list.get(i).get("aname"));
					record.set("aphone", list.get(i).get("aphone"));
					record.set("countGoods", list.get(i).get("countGoods"));
					record.set("countNum", list.get(i).get("countNum"));
					record.set("countMoney", list.get(i).get("countMoney"));
						if(list.get(i).get("qstate").equals(1)){
							if(list.get(i).get("state").equals(1)){
								record.set("qstate", "上线审核中");
							}else if(list.get(i).get("state").equals(2)){
								record.set("qstate", "上线");
							}else if(list.get(i).get("state").equals(3)){
								record.set("qstate", "下线");
							}else{
								record.set("qstate", "驳回");
							}
						}if(list.get(i).get("qstate").equals(2)){
							record.set("qstate", "禁货");
						} if(list.get(i).get("qstate").equals(3)){
							record.set("qstate", "降权");
						}
					
					//record.set("createDate", list.get(i).get("createDate"));
				listz.add(record);
	 		}
			// 导出
			storerTongService.export(getResponse(), getRequest(),titles, listz);		
			logger.info( "成功");
			renderNull();
		}
		
		
	 
	 /**
	  * 编辑分公司
	  */
		public void toEdit(){
			String storeid = getPara("storeid");
			if(!StrKit.isBlank(storeid)){
				setAttr("object", storerTongService.findById(storeid));
			}
			render("edit.jsp");
		}
		public void edit(){
			String storeid=getPara("storeid");
			int qstate=getParaToInt("qstate",0);
			if(!StrKit.isBlank(storeid)&&qstate!=0){
				Store store=storerTongService.findByStoreId(storeid);
				store.set("qstate", qstate);
				store.set("checktime",  DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
				if(storerTongService.edit(store)){
					renderSuccess();
					return;
				}else{
					renderResult(1);
					return;
				}
			}else{
				renderResult(1);
				return;
			}
		}
		/**
		 * 商品订单
		 */
		public void goodsOrder(){
			String storeid = getPara("storeid");
			set("storeid", storeid);
			render("goodsOrder.jsp");
		}
		/**
		 * 商品订单
		 */
		public void goodsOrdergb(){
			String storeid = getPara("storeid");
			set("storeid", storeid);
			render("goodsOrdergb.jsp");
		}
		public void doDetail() {
			int userid = getCurrentUser().getInt("id");
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			// 商品id
			String storeid = getPara("storeid");
			System.out.println("获取storeid"+storeid);
			renderResult(0, storerTongService.findOrderById(pageNum, pageSize, storeid));
		}
		public void doDetailgb() {
			int userid = getCurrentUser().getInt("id");
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			// 商品id
			String storeid = getPara("storeid");
			
			renderResult(0, storerTongService.findOrderByIdgb(pageNum, pageSize, storeid));
		}
}
